from schema import And, Or, Schema, Use, Optional as SchemaOptional
from tornado.escape import utf8, to_unicode
from decimal import Decimal
import re

import arrow
import datetime

schema_utf8_multi = And(Use(utf8), Use(
    lambda x: [_.strip() for _ in x.split(',') if _ != ""]))
schema_unicode_multi = And(Use(utf8), Use(
    lambda x: [to_unicode(_.strip()) for _ in x.split(',') if _ != ""]))
schema_utf8 = And(Use(utf8), len)
schema_utf8_empty = Use(utf8)  # 允许为空的utf8
schema_unicode = And(Use(to_unicode), len, error='请传入不为空的字符串')
schema_unicode_empty = Use(to_unicode, error='请传入合法的字符串')
schema_unicode_upper = And(Use(utf8), len, Use(str.upper), Use(
    lambda x: x.decode(b'utf-8')), error='请传入合法的字符串')
schema_unicode_strip = And(Use(str.strip), schema_unicode)
schema_unicode_title = And(schema_unicode_strip, Use(str.title))
schema_int = Use(int, error='请传入合法的整数')
schema_int_positive = And(Use(int), lambda x: x > 0, error='请传入合法的正整数')
schema_int_non_negtive = And(Use(int), lambda x: x >= 0, error='请传入合法的非负整数')
schema_int_multi = And(Use(utf8), Use(lambda x: [int(_.strip()) for _ in x.split(b',') if _ != ""]),
                       error='请传入以逗号分隔的整数')
schema_float = And(Use(float),
                   Use(lambda x: round(x, 10)), error='')
schema_decimal = And(Use(Decimal))
schema_float_empty = And(Use(lambda x: 0 if x == '' else x), schema_float)
schema_float_2 = And(Use(float), Use(lambda x: round(x, 2)))
# schema_fh_base = And(Use(float), Use(lambda x: round(x, 2)), lambda x: 0 < x < 100)
schema_bool = And(Use(int), Use(bool))
schema_objectid = And(schema_unicode, lambda x: len(x) == 24)
schema_date = And(Use(utf8), Use(arrow.get), Use(lambda x: x.date()))
schema_date_arrow = And(Use(utf8), Use(arrow.get),
                        error='请传入合法的日期, 如:2017-05-20')
schema_datetime = And(Use(utf8), Use(arrow.get), Use(lambda x: x.datetime))
schema_hhmm = And(Use(utf8), Use(lambda x: datetime.time(*list(map(int, x.split(':'))))),
                  Use(lambda x: x.strftime(b"%H:%M")))

email_pattern = re.compile(r'[^@]+@[^@]+\.[^@]+')
schema_email_empty = And(Use(to_unicode), lambda x: email_pattern.search(x))



from json_test import action

schema_sku_attr = Schema([schema_int])

schema_sku_del = Schema([schema_int])

schema_sku_sku_action = Schema(
	{
	"action":{
	"delete" : {
		schema_sku_del
	}
	}
})

param = {
	"add": [
		{
			"attr_id" : 827322,
			"value": "value1",
			"name": "Color" 
		},
		{
			"attr_id" : 827322,
			"value": "value2",
			"name": "Size"
		}
	],
	"delete": ["4267371"],
	"mod": {
		"sku_id": 4267375,
		"skus": [
			{   "attr_id" : 827322,
	            "name": "Color",
				"value": "Gary",
				"value_id": 2817378
			},
			{	"attr_id" : 827322,
				"value": "Lwerwe",
				"name": "Size",
				"value_id": 2817381
			}
		]
		
	},

	# "attr": [{
	# 	"name": "Color",
	# 	"attr_id": "827322"
	# }, {
	# 	"name": "Size",
	# 	"attr_id": "827323"
	# }],
	"product_no": "BCS000023401N"
}

a = {
			"sku_id": 4267375,
			"skus": [
				{   "attr_id" : 827322,
		            "name": "Color",
					"value": "Gary",
					"value_id": 2817378
				},
				{	"attr_id" : 827322,
					"value": "Lwerwe",
					"name": "Size",
					"value_id": 2817381
				}
			]
			
		
	}


schema_sku_dict = Schema(
		{   "attr_id" : schema_int,
            "name": schema_unicode,
			"value": schema_unicode,
			SchemaOptional("value_id"): schema_int
		}
	)

schema_modify_sku = Schema(
		{
		"sku_id": schema_int,
		'skus' : [schema_sku_dict]
		} )

mod_dict = {   
			"attr_id" : 827322,
		    "name": "Color",
			"value": "Gary",
			"value_id": 2817378
		}
# mod_dict = schema_sku_dict.validate(mod_dict)

# a = schema_modify_sku.validate(a)

schema_add_sku = Schema([schema_sku_dict])

schema_sku_attr_dict  = Schema(
	{
		"name": schema_unicode,
		"attr_id": schema_int
	})

schema_del_sku = Schema([schema_int])
# del_list = [1, 2, 3]
# print(schema_del_sku.validate(del_list))

schema_fasting_sku_change = Schema({
	"product_no": schema_unicode,
	"add" : SchemaOptional(schema_add_sku),
	"mod" : SchemaOptional(schema_modify_sku),
	"delete" : SchemaOptional(schema_del_sku)
})

# print(schema_fasting_sku_change.validate(param))





"""
schema sku_chage end
"""


attr_dict = {
	"new":[
	{	"attr_id" : 827322, "name": "Color", "value": "Gary", "value_id": 2817378},
	{	"attr_id" : 827322, "name": "Color", "value": "Gary", "value_id": 2817378}	],
	"attr": [
		{	"name":"Color","attr_id": 827322},
		{	"name":"Size","attr_id": ""}
	]
}




schema_sku_attr = Schema([{ 
	"name" : schema_unicode,
	SchemaOptional("attr_id") : Or(schema_int, "", '')
	}])	

schema_sku_new = Schema([
	{   SchemaOptional("attr_id") : Or(schema_int, "", ''),
    	"name": schema_unicode,
		"value": schema_unicode,
		SchemaOptional("value_id"): Or(schema_int, "", '')
	}])

schema_sku_attr_chage = Schema({
		"new" : schema_sku_new,
		"attr": schema_sku_attr
	}
	)

print(schema_sku_attr_chage.validate(attr_dict))